package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.Car;
import com.vehicle.pojo.ViolationRecord;
import com.vehicle.service.ViolationRecordService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(value = "/violationRecord")
public class VolationRecordController extends BasePreciseController {
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private ViolationRecordService violationRecordService;
    @RequestMapping(value = "/violationRecords")
    @ResponseBody
    public DataGridView violationRecordList(ViolationRecord violationRecord,Integer carid){
        Car car = new Car();
        car.setCarid(carid);
        violationRecord.setCars(car);
        DataGridView dataGridView=violationRecordService.findViolationRecord(violationRecord);
        return dataGridView;
    }
    @RequestMapping(value = "/deleteViolationRecord")
    @ResponseBody
    public ResultDto deleteViolationRecord(Integer violationid){
        ResultDto resultDto = new ResultDto();
        try {
            violationRecordService.deleteViolationRecord(violationid);
            resultDto.setCode(200);
            resultDto.setMsg("删除成功");
        } catch (Exception e) {
            resultDto.setMsg("删除失败");
            resultDto.setCode(-200);
        }
        return resultDto;
    }
    @RequestMapping(value = "/addViolationRecord")
    @ResponseBody
    public ResultDto addViolationRecord(ViolationRecord violationRecord){
        ResultDto resultDto = new ResultDto();
        try {
            violationRecordService.addViolationRecord(violationRecord);
            resultDto.setMsg("增加成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("增加失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }
        return resultDto;
    }


    @RequestMapping(value = "/updateViolationRecord")
    @ResponseBody
    public ResultDto updateViolationRecord(ViolationRecord violationRecord){
        ResultDto resultDto = new ResultDto();
        try {
            violationRecordService.updateViolationRecord(violationRecord);
            resultDto.setMsg("修改成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("修改失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }finally {
            return resultDto;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/deleteBatchViolationRecord")
    public ResultDto deleteBatchViolationRecord(@RequestParam("violationIds[]") Integer [] violationIds){
        ResultDto resultDto = new ResultDto();
        try {
            violationRecordService.deleteBatchViolationRecord(violationIds);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("删除失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }finally {
            return resultDto;
        }
    }
}
